package edu.szu.huhanpeng.doodlingFight.model 
{
	import edu.szu.huhanpeng.doodlingFight.model.data.GameData;
	import edu.szu.huhanpeng.doodlingFight.model.data.ScoreData;
	import org.puremvc.as3.interfaces.INotifier;
	import org.puremvc.as3.interfaces.IProxy;
	import org.puremvc.as3.patterns.proxy.Proxy;
	
	/**
	 * ...
	 * @author Huhanpeng
	 */
	public class GameProxy extends Proxy implements IProxy, INotifier 
	{
		public static const NAME:String = "GameProxy";
		
		public function GameProxy(gameData:GameData) 
		{
			super( NAME, gameData );
		}
		
		//游戏模式
		public function setGameMode(value:int):void
		{
			if ( value != GameData.ONE_PLAYER && value != GameData.TWO_PLAYER ) 
			{
				throw new Error("游戏类型设置错误");
			}
			
			gameData.gameMode = value;
		}
		
		public function isOnePlayer():Boolean
		{
			if ( gameData.gameMode == GameData.ONE_PLAYER ) 
			{
				return true;
			}
			return false;
		}
		
		//重置
		public function reset():void
		{
			data = new GameData();
		}
		
		//玩家姓名
		public function setOpName(text:String):void 
		{
			gameData.pOneName = text;
		}
		
		public function setTpName(text:String):void 
		{
			gameData.pTwoName = text;
		}
		
		public function get actingPlayer():int
		{
			return gameData.actionPlayer;
		}	
		
		public function set actingPlayer(value:int):void
		{
			if ( GameData.PLAYERS.indexOf(value) == -1 ) 
			{
				throw new Error("设定值" + value.toString() + "不是玩家代号");
			}
			gameData.actionPlayer = value;
		}
		
		public function turnPlayer():void
		{
			gameData.actionPlayer *= -1;
		}
		
		public function setWinScore(score:int):void 
		{
			if ( score < 0 ) 
			{
				throw new Error("获胜分数必须大于0，不能为" + score);
			}
			gameData.winScore = score;
		}
		
		public function getPlayerName(owner:int):String 
		{
			if ( GameData.PLAYERS.indexOf(owner) == -1 ) 
			{
				throw new Error(owner.toString() + "不是玩家代号");
			}
			
			switch ( owner ) 
			{
				case GameData.P_ONE:
					return gameData.pOneName;
				break;
				
				case GameData.P_TWO:
					return gameData.pTwoName;
				default:
					
			}
			return null as String;
		}
		
		public function getPlayerScore(codeName:int):ScoreData 
		{
			if ( GameData.PLAYERS.indexOf(codeName) == -1 ) 
			{
				throw new Error(codeName.toString() + "不是玩家代号");
			}
			
			switch ( codeName ) 
			{
				case GameData.P_ONE:
					return gameData.pOneScore;
				break;
				
				case GameData.P_TWO:
					return gameData.pTwoScore;
				default:
					
			}
			return null as ScoreData;
		}
		
		public function get winScore():int
		{
			return gameData.winScore;
		}
		
		public function get pOneScoreData():ScoreData 
		{
			return gameData.pOneScore;
		}
		
		public function get pTwoScoreData():ScoreData
		{
			return gameData.pTwoScore;
		}
		
		protected function get gameData():GameData
		{
			return data as GameData;
		}
		
	}

}